package com.xpchenfrank.monitor;

import com.xpchenfrank.entity.WsmItem;
import com.xpchenfrank.entity.WsmNotify;
import com.xpchenfrank.entity.WsmSite;
import com.xpchenfrank.mapper.WsmItemMapper;
import com.xpchenfrank.mapper.WsmNotifyMapper;
import com.xpchenfrank.mapper.WsmSiteMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.security.GeneralSecurityException;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

/**
 * 监控股票价格
 * @author 503811
 *
 */
@Slf4j
@Component
public class StockMonitor {
	
	@Resource
    WsmSiteMapper wsmSiteMapper;

	@Resource
	WsmItemMapper wsmItemMapper;

	@Resource
	WsmNotifyMapper wsmNotifyMapper;
	
	@Scheduled(cron="0 */30 * * * ?")
	public void run() throws GeneralSecurityException {
		
		log.info("股票监控的monitor开始");



        log.info("股票监控的monitor结束");
		
	}
	
	public void checkStock() throws GeneralSecurityException {

        //从表里查出有哪些博客是需要监控的
        List<WsmSite> monitorSites = wsmSiteMapper.listAllAvailableSites();

        for (WsmSite monitorSite : monitorSites) {

            List<WsmItem> items = wsmItemMapper.listBySiteId(monitorSite.getId());

            WebDriver driver = new HtmlUnitDriver();

            if (CollectionUtils.isNotEmpty(items)) {
                for (WsmItem item : items) {

                    // And now use this to visit Google
                    driver.get(item.getUrl());

                    //driver.findElement(By.cssSelector("#food span.dairy.aged"));

                    //查招考简章与公告的信息
                    WebElement foundEle = driver.findElement(By.xpath(item.getXpathKeyword()));

                    String content = foundEle.getText();

                    System.out.println("监控到的内容是 " + content);

                    if (StringUtils.isBlank(item.getOldItem())) {
                        //说明老的记录是空，这是第一次监控这条记录
                        log.info("第一次监控这个项目");

                        item.setOldItem(content);
                        item.setItem(content);
                        item.setLastchecked(LocalDateTime.now());
                        wsmItemMapper.updateByPrimaryKey(item);
                    } else {
                        if (!Objects.equals(item.getOldItem(), content)) {

                            log.info("监控内容发生了变化");

                            //这就代表内容有变化了
                            WsmNotify notify = new WsmNotify();
                            notify.setItemId(item.getId());
                            notify.setCreateTime(LocalDateTime.now());
                            wsmNotifyMapper.insertSelective(notify);

                            item.setOldItem(item.getItem());
                            item.setItem(content);
                            item.setLastchecked(LocalDateTime.now());
                            wsmItemMapper.updateByPrimaryKey(item);

                        }
                    }

                }
            }

            driver.quit();
        }
    }
}
